﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Collections.Specialized;

namespace Hisun.Components
{
    public class HisunContext
    {
        private HttpContext _httpContext = null;
        private NameValueCollection _queryString = null;
        private string _siteUrl = null;
        private Uri _currentUri;
        string _rawUrl;
        bool _isUrlReWritten = false;
        private SiteSettings _SiteSettings = null;

        private UserInfos _UserInfos = null;

        //是否进行URL重写
        public bool IsUrlReWritten
        {
            get
            {
                return _isUrlReWritten;
            }
            set
            {
                _isUrlReWritten = value;
            }
        }

        public static HisunContext Create(HttpContext context, bool isReWritten)
        {
            HisunContext HisunContext = new HisunContext(context);
            HisunContext.IsUrlReWritten = isReWritten;
            SaveContextToStore(GetSlot(), HisunContext);
            return HisunContext;
        }

        private HisunContext(HttpContext context)
        {
            this._httpContext = context;
            Initialize(new NameValueCollection(context.Request.QueryString), context.Request.Url, context.Request.RawUrl, GetSiteUrl());
        }

        private string GetSiteUrl()
        {

            string siturl = "";

            return siturl;

        }

        private void Initialize(NameValueCollection qs, Uri uri, string rawUrl, string siteUrl)
        {
            _queryString = qs;
            _siteUrl = siteUrl;
            _currentUri = uri;
            _rawUrl = rawUrl;
        }

        /// <summary>
        /// 取得站点设置信息
        /// </summary>
        public SiteSettings SiteSettings
        {
            get
            {
                if (_SiteSettings == null)
                    _SiteSettings = SiteSettingManager.GetSiteSetting("SiteUrl");
                return _SiteSettings;
            }
            set
            {
                _SiteSettings = value;
            }
        }

        public UserInfos UserInfos
        {
            get
            {
                if (_UserInfos == null)
                    _UserInfos = GetUserInfo();
                return _UserInfos;
            }
            set
            {
                _UserInfos = value;
                _httpContext.Session["UserInfo"] = _UserInfos;
            }
        }


        private UserInfos GetUserInfo()
        {
            try
            {
                if (_httpContext.Session["UserInfo"] != null)
                {
                    UserInfos ui = (UserInfos)_httpContext.Session["UserInfo"];
                    return ui;
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        public static HisunContext Current
        {
            get
            {
                LocalDataStoreSlot storeSlot = GetSlot();
                HisunContext context = Thread.GetData(storeSlot) as HisunContext;
                if (context == null)
                {
                    HttpContext httpContext = HttpContext.Current;
                    if (httpContext == null)
                        throw new Exception("No HisunContext exists in the Current Application");
                    context = new HisunContext(httpContext);
                    SaveContextToStore(storeSlot, context);
                }
                return context;

            }
        }

        private static LocalDataStoreSlot GetSlot()
        {
            return Thread.GetNamedDataSlot("HisunContextStore");
        }
        private static void SaveContextToStore(LocalDataStoreSlot storeSlot, HisunContext context)
        {
            Thread.SetData(storeSlot, context);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        public void SetCookie(string CookieName, string CookieValue)
        {
            _httpContext.Response.Cookies["HisunCookie"][CookieName] = CookieValue;
            _httpContext.Response.Cookies["HisunCookie"].Expires = DateTime.Now.AddYears(1);

          
        }

        public void SetCookie(string CookieName, string CookieValue,double days)
        {
            _httpContext.Response.Cookies["HisunCookie"][CookieName] = CookieValue;
            _httpContext.Response.Cookies["HisunCookie"].Expires = DateTime.Now.AddMinutes(days);


        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        public string GetCookie(string CookieName)
        {
            string ss;
            if (_httpContext.Request.Cookies["HisunCookie"] == null)
                ss = string.Empty;
            else
                ss = _httpContext.Request.Cookies["HisunCookie"][CookieName];
            return (ss == null) ? string.Empty : ss;
        }

        /// <summary>
        /// 删除Cookie
        /// </summary>
        public void DelCookie(string CookieName)
        {
            //throw new System.NotImplementedException();
            _httpContext.Response.Cookies["HisunCookie"].Expires = DateTime.Now.AddDays(-1);
        }
    }
}
